package org.dromara.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.hotel.domain.TerHotels;
import org.dromara.hotel.domain.bo.TerHotelsBo;
import org.dromara.hotel.domain.vo.TerHotelsVo;
import org.dromara.hotel.mapper.TerHotelsMapper;
import org.dromara.hotel.service.ITerHotelsService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 酒店信息维护Service业务层处理
 *
 * @author xch
 * @date 2025-05-26
 */
@RequiredArgsConstructor
@Service
public class TerHotelsServiceImpl implements ITerHotelsService {

    private final TerHotelsMapper baseMapper;

    /**
     * 查询酒店信息维护
     *
     * @param id 主键
     * @return 酒店信息维护
     */
    @Override
    public TerHotelsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询酒店信息维护列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 酒店信息维护分页列表
     */
    @Override
    public TableDataInfo<TerHotelsVo> queryPageList(TerHotelsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TerHotels> lqw = buildQueryWrapper(bo);
        Page<TerHotelsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    public TableDataInfo<TerHotelsVo> queryHotelByAccess(TerHotelsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TerHotels> lqw = buildQueryWrapper(bo);
        Page<TerHotelsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        if(result.getTotal() <= 0){
            LambdaQueryWrapper<TerHotels> newlqw = Wrappers.lambdaQuery();
            newlqw.orderByAsc(TerHotels::getId);
            newlqw.like(StringUtils.isNotBlank(bo.getCity()), TerHotels::getCity, bo.getCity());
            Page<TerHotelsVo> newResult = baseMapper.selectVoPage(pageQuery.build(), newlqw);
            return TableDataInfo.build(newResult);
        }
        return TableDataInfo.build(result);
    }
    /**
     * 查询符合条件的酒店信息维护列表
     *
     * @param bo 查询条件
     * @return 酒店信息维护列表
     */
    @Override
    public List<TerHotelsVo> queryList(TerHotelsBo bo) {
        LambdaQueryWrapper<TerHotels> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TerHotels> buildQueryWrapper(TerHotelsBo bo) {
        LambdaQueryWrapper<TerHotels> lqw = Wrappers.lambdaQuery();
        if(bo!=null){
            lqw.orderByAsc(TerHotels::getId);
            lqw.like(StringUtils.isNotBlank(bo.getHotelName()), TerHotels::getHotelName, bo.getHotelName());
            lqw.eq(StringUtils.isNotBlank(bo.getHotelAddress()), TerHotels::getHotelAddress, bo.getHotelAddress());
            lqw.eq(StringUtils.isNotBlank(bo.getDescription()), TerHotels::getDescription, bo.getDescription());
            lqw.eq(bo.getHotelStatus() != null, TerHotels::getHotelStatus, bo.getHotelStatus());
            lqw.eq(StringUtils.isNotBlank(bo.getHotelImg()), TerHotels::getHotelImg, bo.getHotelImg());
        }
        return lqw;
    }

    /**
     * 新增酒店信息维护
     *
     * @param bo 酒店信息维护
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TerHotelsBo bo) {
        TerHotels add = MapstructUtils.convert(bo, TerHotels.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改酒店信息维护
     *
     * @param bo 酒店信息维护
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TerHotelsBo bo) {
        TerHotels update = MapstructUtils.convert(bo, TerHotels.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TerHotels entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除酒店信息维护信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


}
